<template>
    <div>
        <template v-if="Value.length">
            <div class="ItemView" v-for="(item, index) in Value">
                <div class="ItemName" :style="Option?.Option?.LabelStyle">{{ Fun? Fun(index): index + 1 + ':' }}
                </div>
                <div style="flex: 1;">
                    <ArrayItem :Type="Type" :Option="Option" :Level="Level + 1" :index="index" :Value="item"
                        @UpdateValue="UpdateValue" />
                </div>
                <div class="Delete" @click="delect(index)">X</div>
            </div>
        </template>
        <div class="empty" v-if="!Value.length">
            暂无数据
        </div>
    </div>
</template>
<script setup>
import { ref } from 'vue';
import ArrayItem from './ArrayItem.vue'

const props = defineProps(['Option', 'DefaultValue', 'Level']);
const Value = ref([]);

function GetContent(Type) {
    var TypeArray = Type.split(':');
    TypeArray.shift();
    return TypeArray.join(':');
}

const Type = GetContent(props.Option.Type);

var Fun = null;

if (props.Option && props.Option.Option && props.Option.Option.Label) {
    Fun = new Function('index', 'return ' + props.Option.Option.Label);
};

function UpdateValue(_Value, index) {
    Value.value[index] = _Value;
}

function delect(index) {
    Value.value.splice(index, 1)
}

const GetValue = function () {
    return JSON.parse(JSON.stringify(Value.value))
}

const SetValue = function () { }

defineExpose({
    add: function () {
        Value.value.push('');
    }, GetValue, SetValue
})


</script>
<style lang="less" scoped>
.ItemView {
    display: flex;
    padding-top: 10px;
    align-items: center;
}

.ItemName {
    width: 80px;

    text-align: right;
    padding-right: 10px;
}

.empty {
    padding: 10px;
    text-align: center;
}

.Delete {
    user-select: none;
    cursor: pointer;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    margin-left: 10px;
    background-color: red;
    color: #fff;
    text-align: center;
}
</style>